package com.sawen.houserent.Mqconfig;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Mqconfig {
    @Bean(name = "message1")
    public Queue queueMessage1(){
        return new Queue("loginMessage");
    }
    @Bean(name = "message2")
    public Queue queueMessage2(){
        return new Queue("registMessage");
    }
    @Bean(name = "message3")
    public Queue queueMessage3(){
        return new Queue("rplMessage");
    }
    @Bean
    public TopicExchange exchange(){
        return new TopicExchange("exchange");
    }
    @Bean
    Binding bindingExchangeMessage1(@Qualifier("message1")Queue queueMessage1,TopicExchange exchange){
        return BindingBuilder.bind(queueMessage1).to(exchange).with("Topic.login");
    }

    @Bean
    Binding bindingExchangeMessage2(@Qualifier("message2")Queue queueMessage2,TopicExchange exchange){
        return BindingBuilder.bind(queueMessage2).to(exchange).with("Topic.regist");
    }
    @Bean
    Binding bindingExchangeMessage3(@Qualifier("message3")Queue queueMessage3,TopicExchange exchange){
        return BindingBuilder.bind(queueMessage3).to(exchange).with("Topic.rpl");
    }



        @Bean
        public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, MessageConverter messageConverter) {
            RabbitTemplate template = new RabbitTemplate(connectionFactory);
            template.setMessageConverter(messageConverter);
            return template;
        }

        @Bean
        public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory, MessageConverter messageConverter) {
            SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
            factory.setConnectionFactory(connectionFactory);
            factory.setMessageConverter(messageConverter);
            return factory;
        }

        @Bean
        public MessageConverter messageConverter() {
            return new ContentTypeDelegatingMessageConverter(new Jackson2JsonMessageConverter());
        }

}
